package com.zgt.web.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.zgt.model.Research;
import com.zgt.model.Topic;
import com.zgt.service.ResearchService;
import com.zgt.service.TopicService;
import com.zgt.util.MD5;
import com.zgt.util.Page;
import com.zgt.util.ToExcel;
import com.zgt.web.AbstractAction;

@Controller("topicAction")
@Scope("prototype")
public class TopicAction extends AbstractAction implements ModelDriven<Topic> {

	private String key;
	private String keyType;
	private MD5 m = new MD5();

	private Page page = new Page();
	private Map request = (Map) ActionContext.getContext().get("request");
	private Research research = new Research();
	private int research_id;
	private ResearchService researchService;
	private Map session = ActionContext.getContext().getSession();

	private String submitFlag;
	private Topic topic = new Topic();
	private TopicService topicService;

	private File uploadExcle;

	private String uploadExcleContentType;
	private String uploadExcleFileName;

	public String add() {
		if (submitFlag == null) {
			return INPUT;
		}

		research.setId(research_id);
		topic.setResearch(research);
		topicService.addTopic(topic);
		request.put("url", "topic_list.action");
		return SUCCESS;
	}

	public String batch() {
		if (submitFlag == null) {
			return INPUT;
		}
		List temList;
		String realpath = (ServletActionContext.getServletContext()
				.getRealPath("/upload"));
		File file = new File(realpath);
		File newFile = new File(file, uploadExcleFileName);
		if (!file.exists())
			file.mkdirs();
		try {
			FileUtils.copyFile(uploadExcle, newFile);

			temList = ToExcel.readExcel(newFile);

			topicService.addTopics(temList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.put("url", "topic_list.action");
		return SUCCESS;
	}

	public String delete() {
		topicService.deleteTopicById(topic.getId());
		return SUCCESS;
	}

	public String edit() {
		if (submitFlag == null) {
			topic = topicService.getTopicById(topic.getId());
			request.put("topic", topic);
			return INPUT;
		}
		research.setId(research_id);
		topic.setResearch(research);
		topicService.updateTopic(topic);
		request.put("url", "topic_list.action");
		return SUCCESS;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	public String getKey() {
		return key;
	}

	public String getKeyType() {
		return keyType;
	}

	public Topic getModel() {
		return topic;
	}

	public Page getPage() {
		return page;
	}

	public int getResearch_id() {
		return research_id;
	}

	public String getSubmitFlag() {
		return submitFlag;
	}

	public File getUploadExcle() {
		return uploadExcle;
	}

	public String getUploadExcleContentType() {
		return uploadExcleContentType;
	}

	public String getUploadExcleFileName() {
		return uploadExcleFileName;
	}

	public String info() {

		topic = topicService.getTopicById(topic.getId());
		request.put("topic", topic);
		return INPUT;

	}

	public String list() {
		request.put("page", topicService.listTopic(page));
		return INPUT;
	}

	public String search() {
		topicService.listSearch(page, key, keyType);
		request.put("key", key);
		request.put("keyType", keyType);
		return INPUT;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public void setKeyType(String keyType) {
		this.keyType = keyType;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public void setResearch_id(int researchId) {
		research_id = researchId;
	}

	@Resource
	public void setResearchService(ResearchService researchService) {
		this.researchService = researchService;
	}

	public void setSubmitFlag(String submitFlag) {
		this.submitFlag = submitFlag;
	}

	@Resource
	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public void setUploadExcle(File uploadExcle) {
		this.uploadExcle = uploadExcle;
	}

	public void setUploadExcleContentType(String uploadExcleContentType) {
		this.uploadExcleContentType = uploadExcleContentType;
	}

	public void setUploadExcleFileName(String uploadExcleFileName) {
		this.uploadExcleFileName = uploadExcleFileName;
	}

	@Override
	public void validate() {
		List<Research> researchs = researchService.listAllResearch();
		request.put("researchs", researchs);

		if (submitFlag == null) {
			return;
		}
		if ("upload".equals(submitFlag)) {
			return;
		}
	}

}
